<?php
class amazon{
    public function uploadObject($data, $return = false, $backcard = false, $generatecard = false) {
        if ($generatecard) {
            $folder_bucket = !empty($this->config ['folder_generated']) ? $this->config['folder_generated'] : '';
        } else {
            $folder_bucket = !empty($this->config ['folder_template']) ? $this->config['folder_template'] : '';
        }
        
        $data['upload_file'] = addslashes($data['upload_file']);
        if (!empty($data['id']) && !$backcard) {
            $stPostcardCard_model = $this->loadmodel("stPostcardCard");
            if ($this->checkUploadFile($data['id'], $data)) {
                if (!$this->deleteObject($data['id'], true)) {
                    return false;
                }
            } else {
                unset ($data['upload_file']);
//                var_dump($data);
                $save_data = array(
                    'id' => $data['id'],
                    'name' => $data['name'],
                    'num_faces' => $data['num_faces'],
                    'cat_id' => $data['cat_id'],
                    'photographer' => $data['photographer'],
                    'description' => $data['description'],
                    'back_card_text' => $data['back_card_text'],
                    'faces' => $data['faces'],
                    'featured' => $data['featured'],
                    'logo_file' => $data['logo_file']
                );
                return $stPostcardCard_model->saveStPostcardCard($data);
            }
        }
        if ($this->amazonS3->if_bucket_exists($data['bucket_name'])) {
            $stPostcardCard_model = $this->loadmodel("stPostcardCard");
            $info = pathinfo($data['upload_file']);
            
            $ext = $info['extension'];
            $id = empty($data['id']) ? $stPostcardCard_model->getLastId() : $data['id'];
            
            if(empty($data['id'])){
                $cdate = date('Y-m-d H:i:s');
            }else{
                $cdate = '';
            }
            
            
            if ($backcard) {
                $filenames[0] = $folder_bucket . '/' . "postcard_back_" . time() . '.' . $ext;
            } else {
                $filenames[0] = $folder_bucket . '/' . "postcard_" . time() . '.' . $ext;
            }

            // upload image to amazon s3
            $img_name = basename($data['upload_file']);
            $img_name = explode('.', $img_name);
            $img_name = $img_name[0];
            $file_large = str_replace($img_name, $img_name . '-large', $data['upload_file']);
//            echo $data['upload_file'];die;
            $this->amazonS3->batch()->create_object($data['bucket_name'], $filenames[0], array(
                'fileUpload' => $file_large,
                'acl' => AmazonS3::ACL_PUBLIC,
//                'meta' => array(
//                    'id' => $id, // x-amz-meta-word
//                    'bucket_id' => $bucket_id, // x-amz-meta-ice-ice-baby
//                    'name' => $data['name'],
////                    'description' => $data['description'],
//                    'cdate' => $cdate,
//                ),
            ));

            // upload thumbnail image to amazon s3

            $thumbnail_file = str_replace($img_name, $img_name . '-thumb', $data['upload_file']);

            if ($backcard) {
                $filenames[1] = $folder_bucket . '/' . "postcard_back_thumb_" . time() . '.' . $ext;
            } else {
                $filenames[1] = $folder_bucket . '/' . "postcard_thumb_" . time() . '.' . $ext;
            }


            $this->amazonS3->batch()->create_object($data['bucket_name'], $filenames[1], array(
                'fileUpload' => $thumbnail_file,
                'acl' => AmazonS3::ACL_PUBLIC,
//                'meta' => array(
//                    'id' => $id, // x-amz-meta-word
//                    'bucket_id' => $bucket_id, // x-amz-meta-ice-ice-baby
//                    'name' => $data['name'] . '-thumb',
//                    'description' => $data['description'],
//                    'cdate' => $cdate,
//                ),
            ));
            $file_upload_response = $this->amazonS3->batch()->send();
            if ($file_upload_response->areOK()) {
                $response = $this->amazonS3->get_bucket_region($data['bucket_name']);
                if ($response->isOK()) {
                    foreach ($filenames as $filename) {
                        if (!empty($response->body) && $response->body != AmazonS3::REGION_US_E1) {
                            if ($response->body == AmazonS3::REGION_EU_W1) {
                                $response->body = 'eu-west-1';
                            }
                            $img_amazon_url[] = 'https://s3-' . $response->body . '.amazonaws.com/' . $data['bucket_name'] . '/' . $filename;
                        } else {
                            $img_amazon_url[] = 'https://s3.amazonaws.com/' . $data['bucket_name'] . '/' . $filename;
                        }
                    }
                }
                if (!empty($data['id'])) {
                    $card_id = $data['id'];
                } else {
                    $card_id = NULL;
                }

                if (empty($data['smt_user_id'])) {
                    $smt_user_id = $this->functions->getCurrentSmtUserId($this->config);
                } else {
                    $smt_user_id = $data['smt_user_id'];
                }

                if (empty($data['user_id'])) {
                    $user_id = 0;
                } else {
                    $user_id = $data['user_id'];
                }

                if ($backcard) {                    
                    $save_data = array(
                        'id' => $card_id,
                        'back_card_file_name' => $filenames[0],
                        'back_thumb_url' => $img_amazon_url[1],
                        'back_url' => $img_amazon_url[0]
                    );
//                    var_dump($save_data);die;
                } else {
                    $save_data = array(
                        'id' => $card_id,
                        'smt_user_id' => $smt_user_id,
                        'user_id' => $user_id,                        
                        'file_name' => $filenames[0],
                        'thumb_url' => $img_amazon_url[1],
                        'url' => $img_amazon_url[0],
                        'folder_name' => $folder_bucket,
//                        'bucket_name' => $data['bucket_name'],
//                        
//                        'template_id' => $data['template_id'],
//                        'name' => $data['name'],
//                        'num_faces' => $data['num_faces'],
//                        'logo_file' => $data['logo_file'],
//                        'description' => $data['description'],
//                        'back_card_text' => $data['back_card_text'],
//                        'ip_creator' => $data['ip_creator'],
//                        'faces' => $data['faces'],
//                        'cat_id' => $data['cat_id'],
//                        'featured' => $data['featured'],
//                        'photographer' => $data['photographer']
                    );     
                    $upload_file = $data['upload_file'];
                    unset ($data['upload_file']);
                    $save_data = array_merge($save_data, $data);
                    if(!empty($cdate)){
                        $save_data['cdate'] = $cdate;
                    }
                }


//                var_dump($save_data);die;

                @unlink($data['upload_file']);
                @unlink($thumbnail_file);
                @unlink($file_large);
                if ($return) {
                    if ($stPostcardCard_model->saveStPostcardCard($save_data)) {
                        $save_data['id'] = $id;
                        return $save_data;
                    } else {
                        return false;
                    }
                } else {
                    return $stPostcardCard_model->saveStPostcardCard($save_data);
                }
            } else {
                unlink($data['upload_file']);
                unlink($thumbnail_file);
                unlink($file_large);
                return false;
            }
        }
    }
}